<?php
namespace app\api\controller\user;

// 底层控制器
use think\Controller;

//引入cookie
use think\Cookie;

class Base extends Controller {
    // 构造函数

    public function __construct() {
        // 手动把父类的方法获取过来
        parent::__construct();

        $this->UserModel = model( 'User.User' );
        $this->ConfigModel = model( 'Config' );
    }

    // 登录

    public function login() {
        if ( $this->request->isPost() ) {

            $email = $this->request->param( 'email', '' );
            $password = $this->request->param( 'password', '' );

            $userInfo = $this->UserModel->where( [ 'email' => $email ] )->find();

            // 判断用户是否存在
            if ( !$userInfo ) {
                $this->error( '用户不存在，请重新输入' );
                exit;
            }

            // 密码加密
            $password = md5( $password.$userInfo[ 'salt' ] );

            if ( $password != $userInfo[ 'password' ] ) {
                $this->error( '密码错误，请重新输入' );
                exit;
            }

            //组装数据
            $data = [
                'id'=>$userInfo[ 'id' ],
                'email'=>$userInfo[ 'email' ],
                'nickname'=>$userInfo[ 'nickname' ],
                'avatar'=>$userInfo[ 'avatar_cdn' ],
                'sex'=>$userInfo[ 'sex' ],
                'province'=>$userInfo[ 'province' ],
                'city'=>$userInfo[ 'city' ],
                'district'=>$userInfo[ 'district' ],
                'createtime'=>$userInfo[ 'createtime' ],
                'content'=>$userInfo[ 'content' ],
                'point'=>$userInfo[ 'point' ],
                'vip'=>$userInfo[ 'vip' ],
                'auth'=>$userInfo[ 'auth' ],
                'cover'=>$userInfo[ 'cover_cdn' ],
                'region'=>$userInfo[ 'region' ],
            ];

            //跳转界面
            $this->success( '登录成功', '/user/base/index', $data );
            exit;
        }
    }

    // 身份验证

    public function check() {
        if ( $this->request->isPost() ) {
            $id = $this->request->param( 'id', '' );

            $userinfo = $this->UserModel->find( $id );

            //当没有找到用户的情况下
            if ( !$userinfo ) {
                $this->error( '用户不存在，请去注册' );
                exit;
            }

            //组装数据
            $data = [
                'id'=>$userinfo[ 'id' ],
                'email'=>$userinfo[ 'email' ],
                'nickname'=>$userinfo[ 'nickname' ],
                'avatar'=>$userinfo[ 'avatar_cdn' ],
                'sex'=>$userinfo[ 'sex' ],
                'province'=>$userinfo[ 'province' ],
                'city'=>$userinfo[ 'city' ],
                'district'=>$userinfo[ 'district' ],
                'createtime'=>$userinfo[ 'createtime' ],
                'content'=>$userinfo[ 'content' ],
                'point'=>$userinfo[ 'point' ],
                'vip'=>$userinfo[ 'vip' ],
                'auth'=>$userinfo[ 'auth' ],
                'cover'=>$userinfo[ 'cover_cdn' ],
                'region'=>$userinfo[ 'region' ],
            ];

            //跳转界面
            $this->success( '登录成功', '/user/base/index', $data );
            exit;
        }
    }

    public function logincheck() {
        if ( $this->request->isPost() ) {

            $id = $this->request->param( 'id', '' );

            $userinfo = $this->UserModel->find( $id );

            //当没有找到用户的情况下
            if ( !$userinfo ) {
                $this->error( '用户不存在，请去注册' );
                exit;
            }

            //跳转界面
            $this->success( '登录成功' );
            exit;
        }
    }

    // 修改用户信息

    public function profile() {
        if ( $this->request->isPost() ) {
            // 获取前端传来的数据
            $params = $this->request->param();

            // 获取传来的用户ID
            $userId = $this->request->param( 'id', 0 );

            // 查询数据库是否有该用户
            $userInfo = $this->UserModel->find( $userId );

            if ( !$userInfo ) {
                $this->error( '用户不存在，请重新输入！' );
                exit;
            }

            $province = $this->request->param( 'province', '' );
            $city = $this->request->param( 'city', '' );
            $district = $this->request->param( 'district', '' );
            $content = $this->request->param( 'content', '' );

            // 组装数据
            $data = [
                'id' => $userId,
                'nickname' => $params[ 'nickname' ],
                'province' => isset( $province ) ? $province : '',
                'city' => isset( $city ) ? $city : '',
                'district' => isset( $district ) ? $district : '',
                'sex' => $params[ 'sex' ],
                'content' => isset( $content ) ? $content : '',
            ];

            $password = $this->request->param( 'password', '' );

            // 修改密码
            if ( $password ) {
                $data[ 'password' ] = md5( $params[ 'password' ].$userInfo[ 'salt' ] );
            }

            // 头像上传
            if ( isset( $_FILES[ 'avatar' ] ) && $_FILES[ 'avatar' ][ 'size' ] > 0 ) {

                $avatarUrl = upload( 'avatar' );
                if ( $avatarUrl == false ) {
                    $this->error( '修改失败' );
                    exit;
                }

                // 组装头像信息
                $data[ 'avatar' ] = 'uploads/'.$avatarUrl;
            }

            // 封面上传
            if ( isset( $_FILES[ 'cover' ] ) && $_FILES[ 'cover' ][ 'size' ] > 0 ) {

                $coverUrl = upload( 'cover' );
                if ( $coverUrl == false ) {
                    $this->error( '修改失败' );
                    exit;
                }

                // 组装封面信息
                $data[ 'cover' ] = 'uploads/'.$coverUrl;
            }

            // 更新数据
            $result = $this->UserModel->isUpdate()->save( $data );

            // 判断数据是否更新
            if ( $result === false ) {
                // 更新失败
                // 删除上传上来的图片
                @is_file( $data[ 'cover' ] ) && @unlink( $data[ 'cover' ] );
                @is_file( $data[ 'avatar' ] ) && @unlink( $data[ 'avatar' ] );

                $this->error( '修改失败！' );
                exit;
            } else {
                // 更新成功
                // 删除旧的图片
                if ( @is_file( $data[ 'avatar' ] ) ) {
                    $avatar = ltrim( $userInfo[ 'avatar' ], '/' );
                    @is_file( $avatar ) && @unlink( $avatar );
                }

                if ( @is_file( $data[ 'cover' ] ) ) {
                    $cover = ltrim( $userInfo[ 'cover' ]. '/' );
                    @is_file( $cover ) && @unlink( $cover );
                }
            }

            $user = $this->UserModel->find( $userId );

            //组装数据
            $data = [
                'id'=>$user[ 'id' ],
                'email'=>$user[ 'email' ],
                'nickname'=>$user[ 'nickname' ],
                'avatar'=>$user[ 'avatar_cdn' ],
                'sex'=>$user[ 'sex' ],
                'province'=>$user[ 'province' ],
                'city'=>$user[ 'city' ],
                'district'=>$user[ 'district' ],
                'createtime'=>$user[ 'createtime' ],
                'content'=>$user[ 'content' ],
                'point'=>$user[ 'point' ],
                'vip'=>$user[ 'vip' ],
                'auth'=>$user[ 'auth' ],
                'cover'=>$user[ 'cover_cdn' ],
                'region'=>$user[ 'region' ],
            ];

            //跳转界面
            $this->success( '修改成功！', '/user/base/index', $data );
            exit;
        }
        return '123';
    }

    public function register() {
        if ( $this->request->isPost() ) {
            // 获取输入的值
            $params = $this->request->param();
            // 获取输入的密码
            $password = $params[ 'password' ];
            // 获取输入的确认密码
            $rePwd = $params[ 'repassword' ];
            // 获取邮箱
            $email = $params[ 'email' ];
            // 获取昵称
            $name = $params[ 'nickname' ];

            // 判断 密码 和 确认密码 是否一致
            if ( $password != $rePwd ) {
                $this->error( '密码不一致！' );
                exit;
            }

            // 生成密码盐
            $salt = randstr( 10 );

            // 密码加密
            $password = md5( $password.$salt );

            // 获取系统配置表里面的赠送积分 value 查询某个字段的值
            $RegisterPoint = $this->ConfigModel->where( [ 'key' => 'RegisterPoint' ] )->value( 'value' );

            // 判断注册积分 是不是一个整数 is_numeric() 函数用于检测变量是否为数字或数字字符。
            $RegisterPoint = is_numeric( $RegisterPoint ) ? abs( $RegisterPoint ) : 0;

            // 组装数据
            $data = [
                'email' => $email,
                'nickname' => $name,
                'password' => $password,
                'salt' => $salt,
                'point' => $RegisterPoint,
                'vip' => 0,
                'auth' => 0
            ];

            // 开始插入数据库 validate
            $res = $this->UserModel->validate( 'common/User/User' )->save( $data );

            // getLastSql() 输出查询语句
            // echo $this->UserModel->getLastSql();

            if ( $res === FALSE ) {
                // 注册失败
                // getError 返回操作的结果
                $this->error( $this->UserModel->getError() );
                exit;
            } else {
                // 注册成功，跳转到登录界面
                $this->success( '注册成功', '/user/base/login', $data );
                exit;
            }
            exit;
        }
    }

}

?>